import Vue from 'vue'
import VueI18n from 'vue-i18n'
import Cookies from 'js-cookie'
import elementEnLocale from 'element-ui/lib/locale/lang/en' // element-ui lang
import elementZhLocale from 'element-ui/lib/locale/lang/zh-CN'// element-ui lang
import enCommon from './en_US/common'
import zhCommon from './zh_CN/common'
import zhValidate from './zh_CN/validateLocal'
import enValidate from './en_US/validateLocal'

Vue.use(VueI18n)
const _import = require('@/router/_import_addRouter')

function getMessages() {
  const messages = {
    en_US: {
      ...enCommon,
      ...elementEnLocale,
      ...enValidate
    },
    zh_CN: {
      ...zhCommon,
      ...elementZhLocale,
      ...zhValidate
    }
  }
  if (config.I18_PATH.zh_CN && config.I18_PATH.zh_CN.length > 0) {
    config.I18_PATH.zh_CN.forEach(path => {
      const i18 = _import(path)
      if (i18) {
        const tmp = Object.assign(messages.zh_CN, i18)
        messages.zh_CN = tmp
      }
    })
  }
  if (config.I18_PATH.en_US && config.I18_PATH.en_US.length > 0) {
    config.I18_PATH.en_US.forEach(path => {
      const i18 = _import(path)
      if (i18) {
        const tmp = Object.assign(messages.en_US, i18)
        messages.en_US = tmp
      }
    })
  }
  return messages
}
const messages = getMessages()

const i18n = new VueI18n({
  locale: Cookies.get('language') || 'en_US', // set locale
  messages // set locale messages
})

export default i18n
